/**
 * Created by cleverdou on 2018/7/27.
 */
'use strict';


//在线的socket对象  时间戳为key socket 为value
const onlineSocketObj = {};


// 在线mock数量
let onlineMocks = 0;

//mock数据的集合
const mockDataDic = {};

//当前mock的环境
const currentMockEnvironment = {};

function messageSend(socketId, msg) {
    if (onlineSocketObj[socketId] === undefined) {
        return;
    }
    onlineSocketObj[socketId].emit('message', msg)
}

function createSocketIO(server) {
    const io = require('socket.io')(server);

    io.on('connection', function (socket) {

        let socketID;

        //增加一个mock用户
        socket.on('login', function ({socketID: _socketId}) {
            socketID = _socketId;
            console.log('新增了一个用户id为：', socketID, ' 当前用户总数:', onlineMocks + 1);
            /**onlineSocketObj 数据结构
             *
             * socketID ： socket
             */
            onlineSocketObj[socketID] = socket;
            onlineMocks++;

        });





        //用户选择的mock环境
        socket.on('userSelectMockEnv', function (env) {
            console.log('用户选择的mock环境', env, socketID);

            /**
             * 环境改变 对应的socketID 对应的数据清空
             * */
            mockDataDic[socketID] = {};

            /** currentMockEnvironment 数据结构
             * {
             * socketID : https://sit.nonobank.com
             * }
             * */
            currentMockEnvironment[socketID] = env;
        });

        //减少一个mock用户
        socket.on('disconnect', function () {
            console.log('减少了一个用户id为：', socketID, ' 当前用户总数:', onlineMocks - 1);

            delete onlineSocketObj[socketID];
            delete mockDataDic[socketID];
            delete currentMockEnvironment[socketID];
            onlineMocks--;
            // socket.emit('disconnect',true);
        });

        // 开启mock
        socket.on('mockDataMsg', function (data) {
            console.log('！！！！！！！来自客户端的数据---id', socketID, data.mockKey, data.mockValue, data.mockTag);
            /**mockDataDic 数据结构
             * {
             *  "socketid": {
             *           "path": {
             *               'data':{},
             *              'mockTag':''
             *           },
             *           ...
             *       }
             *      }
             * */
            try {
                if (mockDataDic[socketID] === undefined) {
                    mockDataDic[socketID] = {};
                    const dic1 = {
                        'data': data.mockValue,
                        'mockTag': data.mockTag
                    };
                    mockDataDic[socketID][data.mockKey] = dic1;
                } else {
                    const dic1 = {
                        'data': data.mockValue,
                        'mockTag': data.mockTag
                    };
                    mockDataDic[socketID][data.mockKey] = dic1;
                }
                socket.emit('mockSucced', true);
            }
            catch (e) {
                socket.emit('mockSucced', false);
            }
        });

        //关闭mock
        socket.on('closeMock', function (data) {
            try {
                if (mockDataDic[socketID] === undefined) {
                    mockDataDic[socketID] = {};
                    socket.emit('closeMockSucced', false);
                    return;
                }
                delete  mockDataDic[socketID][data.mockKey];
                socket.emit('closeMockSucced', true);
            }
            catch (e) {
                socket.emit('closeMockSucced', false);
            }
        })

    });
}


module.exports.messageSend = messageSend;
module.exports.onlineSocketObj = onlineSocketObj;
module.exports.mockDataDic = mockDataDic;
module.exports.currentMockEnvironment = currentMockEnvironment;
module.exports.createSocketIO = createSocketIO;